/*
 * 文件名： YuGaoAdapter.java
 * 描    述：预告
 * 时    间：2015-3-25 下午5:47:21
 * 版    本：V1.0
 */

package com.miguo.adapter;

import android.app.Activity;
import android.util.DisplayMetrics;
import android.view.Display;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.animation.Animation;
import android.view.animation.Animation.AnimationListener;
import android.view.animation.AnimationUtils;
import android.widget.RelativeLayout;

import com.miguo.app.AppContext;
import com.miguo.bean.GoodsBean;
import com.miguo.db.GoodsDB;
import com.miguo.framework.adapter.BaseAdapterHelper;
import com.miguo.framework.adapter.QuickAdapter;
import com.miguo.framework.utils.Log;
import com.miguo.framework.utils.Toast;
import com.miguo.framework.view.LazyImageView;
import com.miguo.ui.R;
import com.miguo.utils.DateUtils;
import com.miguo.utils.LocalNotiUtils;
import com.miguo.utils.Options;
import com.miguo.utils.ScreenUtils;
import com.miguo.utils.StringUtils;
import com.mrper.util.DeviceUtil;

public class YuGaoAdapter extends QuickAdapter<GoodsBean> {

    private boolean isSetTip = false;

    private Activity mActivity;

    public YuGaoAdapter(Activity activity) {
        super(activity, R.layout.list_goods_notice);
        mActivity = activity;
    }

    @Override
    protected void convert(final BaseAdapterHelper helper, final GoodsBean item) {
        RelativeLayout imageRl = helper.getView(R.id.goods_image_rl);
        imageRl.getLayoutParams().height = mesureImage();

        helper.setText(R.id.goods_title_tv, item.getTitle());

        String CPrice;
        if (StringUtils.isNull(item.getPhone_price())) {
            CPrice = item.getCoupon_price();
        } else {
            CPrice = item.getPhone_price();
        }
        helper.setText(R.id.goods_curprice_tv, CPrice);

        helper.setText(R.id.goods_volume_tv, item.getVolume() + "");

        helper.setText(R.id.goods_remind_time_tv, DateUtils.getStrTime(
                item.getBegin_time(), "yyyy-MM-dd HH:mm:ss")
                + " 开抢");

        if (isFavor(item)) {
            helper.setImageResource(R.id.goods_favor_iv, R.drawable.goods_favor_sel);
            helper.setVisible(R.id.goods_remind_sel_iv, View.VISIBLE);
        } else {
            helper.setImageResource(R.id.goods_favor_iv, R.drawable.goods_favor_nor);
            helper.setVisible(R.id.goods_remind_sel_iv, View.GONE);
        }

        OnClickListener imgClick = new OnClickListener() {
            @Override
            public void onClick(View v) {
                v.performClick();
            }
        };
        LazyImageView img = helper.getView(R.id.goods_pic_iv);
        img.setNoImgMode(AppContext.isNoImg);
        img.autoDisplayImage(item.getPic_url(),
                Options.getGoodsRoundedOptions(), imgClick);

        helper.getView().setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View view) {
                view.setClickable(false);
                if (isFavor(item)) {
                    favorCancleAction(helper, item);
                } else {
                    favorAction(helper, item);
                }
            }

        });
    }

    private void showRemindView(final BaseAdapterHelper helper) {
        helper.setVisible(R.id.goods_remind_nor_iv, View.VISIBLE);
        Animation remind = AnimationUtils.loadAnimation(context,
                R.anim.remind_icon_out);
        helper.startAnimation(R.id.goods_remind_nor_iv, remind);

        remind.setAnimationListener(new AnimationListener() {
            public void onAnimationEnd(Animation animation) {
                helper.getView().setClickable(true);
                helper.setVisible(R.id.goods_remind_nor_iv, View.GONE);
                helper.setVisible(R.id.goods_remind_sel_iv, View.VISIBLE);
                helper.setImageResource(R.id.goods_favor_iv, R.drawable.goods_favor_sel);
            }

            public void onAnimationRepeat(Animation animation) {
            }

            public void onAnimationStart(Animation animation) {
            }
        });
    }

    private void hideRemindView(final BaseAdapterHelper helper) {
        Animation remind = AnimationUtils.loadAnimation(context,
                R.anim.remind_icon_in);
        helper.startAnimation(R.id.goods_remind_sel_iv, remind);
        remind.setAnimationListener(new AnimationListener() {
            public void onAnimationEnd(Animation animation) {
                helper.getView().setClickable(true);
                helper.setVisible(R.id.goods_remind_sel_iv, View.GONE);
                helper.setImageResource(R.id.goods_favor_iv, R.drawable.goods_favor_nor);
            }

            public void onAnimationRepeat(Animation animation) {
            }

            public void onAnimationStart(Animation animation) {
            }
        });
    }

    private void favorAction(BaseAdapterHelper helper, GoodsBean item) {
        GoodsDB.getInstance(context).save(item);
        isSetTip = true;
        showRemindView(helper);
        LocalNotiUtils.setLocalNoti(context, item);
        Toast.show(context, "设置成功,系统将提前5分钟通知您");
        return;
        // view.setClickable(true);
    }

    private void favorCancleAction(BaseAdapterHelper helper, GoodsBean item) {

        GoodsDB.getInstance(context).delete(item.getItem_id(), 1);
        isSetTip = false;
        hideRemindView(helper);
        LocalNotiUtils.cancelLocalNoti(context, item);
        Toast.show(context, "该宝贝开卖提醒已关闭");
        return;
        // view.setClickable(true);
    }

    private boolean isFavor(GoodsBean item) {
        try {
            isSetTip = GoodsDB.getInstance(context).isSave(item.getItem_id(),
                    1);
        } catch (Exception e) {
            Log.d("" + e.getMessage());
        }
        return isSetTip;
    }

    private int mesureImage() {
        DisplayMetrics displayMetrics = new DisplayMetrics();
        Display display = mActivity.getWindowManager().getDefaultDisplay();
        display.getMetrics(displayMetrics);
        int mPageWidth = DeviceUtil.getScreenWidth(context);
        return (mPageWidth - ScreenUtils.dip2px(mActivity, 21.0f)) / 2;//* 185 / 285;
    }

}
